using System;
using System.Collections.Generic;
using System.Text;

namespace PastCrew.Games.Chess.Engine.Entities.Pieces
{
    public sealed class Knight : BasePiece
    {

        #region constructors

        /// <summary>
        /// 
        /// </summary>
        public Knight()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pieceColor"></param>
        public Knight(Color pieceColor)
        {
            base.Color = pieceColor;
        }

        #endregion

        #region public methods

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public sealed override List<Entities.SquareStatus> GetValidMoves()
        {
            #region knight rule set

            Board.Position currentPosition = this.CurrentSquare.Position;
            List<Entities.SquareStatus> validPositions = new List<Entities.SquareStatus>();
            
            //  XT      X       TX       X
            //  X       X        X       X
            //  X       XT       X      TX
            //
            if(currentPosition.X <= 6)
            {
                // Jump forward 2, right 1
                if (currentPosition.Y <= 5)
                {
                    Board.Square square2F1R = Board.GetSquare((currentPosition.X + 1), (currentPosition.Y + 2));
                    if (!square2F1R.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square2F1R, MoveResult.valid);
                        validPositions.Add(squareStat);                        
                    }
                    else
                    {
                        if (square2F1R.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square2F1R, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                    }
                }
                // Jump backward 2, right 1
                if (currentPosition.Y >= 2)
                {
                    Board.Square square2B1R = Board.GetSquare((currentPosition.X + 1), (currentPosition.Y - 2));
                    if (!square2B1R.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square2B1R, MoveResult.valid);
                        validPositions.Add(squareStat);                                                
                    }
                    else
                    {
                        if(square2B1R.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square2B1R, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);                                                
                        }
                    }
                }
            }            
            if (currentPosition.X >= 1)
            {
                // Jump forward 2, left 1
                if (currentPosition.Y <= 5)
                {
                    Board.Square square2F1L = Board.GetSquare((currentPosition.X - 1), (currentPosition.Y + 2));
                    if (!square2F1L.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square2F1L, MoveResult.valid);
                        validPositions.Add(squareStat);                        
                    }
                    else
                    {
                        if(square2F1L.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square2F1L, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);                        
                        }
                    }
                }
                // Jump backward 2, left 1
                if (currentPosition.Y >= 2)
                {
                    Board.Square square2B1L = Board.GetSquare((currentPosition.X - 1), (currentPosition.Y - 2));
                    if (!square2B1L.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square2B1L, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square2B1L.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square2B1L, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                    }
                }
            }

            //  XXX     X       XXX       X
            //  X       XXX       X     XXX
            //
            if (currentPosition.X <= 5)
            {
                //Jump 1 forward, 2 right
                if (currentPosition.Y <= 6)
                {
                    Board.Square square1F2R = Board.GetSquare((currentPosition.X + 2 ), (currentPosition.Y + 1));
                    if (!square1F2R.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square1F2R, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if(square1F2R.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square1F2R, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                    }
                }
                // Jump 1 backward, 2 right
                if (currentPosition.Y >= 1)
                {
                    Board.Square square1B2R = Board.GetSquare((currentPosition.X + 2), (currentPosition.Y - 1));
                    if (!square1B2R.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square1B2R, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square1B2R.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square1B2R, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                    }
                }
            }
            if (currentPosition.X >= 2)
            {
                // Jump 1 forward, 2 left
                if (currentPosition.Y <= 6)
                {
                    Board.Square square1F2L = Board.GetSquare((currentPosition.X - 2), (currentPosition.Y + 1));
                    if (!square1F2L.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square1F2L, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square1F2L.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square1F2L, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                    }
                }
                // Jump 1 backward, 2 left
                if (currentPosition.Y >= 1)
                {
                    Board.Square square1B2L = Board.GetSquare((currentPosition.X - 2), (currentPosition.Y - 1));
                    if (!square1B2L.Occupied)
                    {
                        Entities.SquareStatus squareStat = new SquareStatus(square1B2L, MoveResult.valid);
                        validPositions.Add(squareStat);
                    }
                    else
                    {
                        if (square1B2L.Occupant.Color != this.Color)
                        {
                            Entities.SquareStatus squareStat = new SquareStatus(square1B2L, MoveResult.valid & MoveResult.strike);
                            validPositions.Add(squareStat);
                        }
                    }
                }
            }

            



            #endregion

            return validPositions;
        }

        #endregion
    }
}
